/*
 * CMediaUpdater.cpp
 *
 *  Created on: 2010-9-2
 *      Author: angelstone
 */

#include <wx/wx.h>

#include "MediaHouseApp.h"
#include "MediaUpdater.h"

#include "soci.h"
#include "soci-sqlite3.h"
#include <sqlite3.h>

#include "mhdefs.h"
#include "mhcontext.h"
#include "mhcategory.h"
#include "mhmedia.h"

#include "addon/mhaddon.h"
#include "addon/mhmediaaddon.h"
#include "addon/mhscanneraddon.h"

CMediaUpdater::CMediaUpdater(mediahouse::CMHContext * p_context,
		void * p_cookie) :
	wxThread(wxTHREAD_JOINABLE), m_pContext(p_context), m_pCookie(p_cookie) {
}

CMediaUpdater::~CMediaUpdater() {
}

wxThread::ExitCode CMediaUpdater::Entry() {

	do {
		if (TestDestroy())
			break;

		ProcessDirs();

		if (TestDestroy())
			break;

		ProcessFiles();

	} while (false);

	return (wxThread::ExitCode) 0;
}

bool CMediaUpdater::ProcessDirs() {
	try {
		std::string sql = "";
		sql += "select t1.hiberlite_id, t1.m_Location ";
		sql
				+= "from CMHMedia T1, CMHMedia_CMHPropertyContainer_m_Properties_items T2 ";
		sql
				+= "where t1.hiberlite_id = t2.hiberlite_parent_id and t2.item_key=:key and t2.item_value=:value";

		if (TestDestroy())
			return false;

		wprintf(L"Process Dir sql:%s\n", (const char *)sql.c_str());

		std::string key("directory");
		std::string value("true");

		soci::rowset<soci::row>
				rs =
						(m_pContext->GetSqlSession()->prepare << sql, soci::use(key), soci::use(
								value));

		soci::rowset<soci::row>::iterator it;
		std::vector<int> deleteIds;

		for (it = rs.begin(); it != rs.end(); it++) {
			if (TestDestroy())
				return false;

			soci::row const & r = *it;
			wxString path = wxString::FromUTF8(r.get<std::string>(1).c_str());

			wprintf(L"process media update dir:%s\n", (const wchar_t *)path.c_str());

			if (wxDirExists(path)) {
				m_pContext->ScanItem(path, m_pCookie);
			} else {
				deleteIds.push_back(r.get<int>(0));
			}
		}

		std::vector<int>::iterator it1;

		for (it1 = deleteIds.begin(); it1 != deleteIds.end(); it1++) {
			if (TestDestroy())
				return false;

			hiberlite::sqlid_t id = *it1;
			hiberlite::bean_ptr<mediahouse::CMHMedia> media =
					m_pContext->GetDatabase()->loadBean<mediahouse::CMHMedia> (id);

			wprintf(L"process media delete dir:%s\n", (const wchar_t *)media->GetLocation().c_str());
			media.destroy();
		}
	} catch (soci::soci_error const & e) {
		wxLogError
			(_("MediaUpdater::ProcessDir:%s"), e.what());
		return false;
	}

	return true;
}

bool CMediaUpdater::ProcessFiles() {
	try {
		std::string sql = "";
		sql += "select t1.hiberlite_id, t1.m_Location ";
		sql
				+= "from CMHMedia t1, CMHMedia_CMHPropertyContainer_m_Properties_items t2 ";
		sql
				+= "where t1.hiberlite_id = t2.hiberlite_parent_id and t2.item_key=:key and t2.item_value=:value";

		wprintf(L"Process File sql:%s\n", (const char *)sql.c_str());

		if (TestDestroy())
			return false;

		std::string key("file");
		std::string value("true");

		soci::rowset<soci::row>
				rs =
						(m_pContext->GetSqlSession()->prepare << sql, soci::use(key), soci::use(
								value));

		if (TestDestroy())
			return false;

		soci::rowset<soci::row>::iterator it;
		std::vector<int> deleteIds;

		for (it = rs.begin(); it != rs.end(); it++) {
			if (TestDestroy())
				return false;

			soci::row const & r = *it;
			wxString path = wxString::FromUTF8(r.get<std::string> (1).c_str());

			wprintf(L"process media update file:%s\n", (const wchar_t *)path.c_str());

			if (wxFileExists(path)) {
				m_pContext->ScanItem(path, m_pCookie);
			} else {
				deleteIds.push_back(r.get<int> (0));
			}
		}

		if (TestDestroy())
			return false;

		std::vector<int>::iterator it1;

		for (it1 = deleteIds.begin(); it1 != deleteIds.end(); it1++) {
			if (TestDestroy())
				return false;

			hiberlite::sqlid_t id = *it1;
			hiberlite::bean_ptr<mediahouse::CMHMedia> media =
					m_pContext->GetDatabase()->loadBean<mediahouse::CMHMedia> (id);

			wprintf(L"process media delete file:%s\n", (const wchar_t *)media->GetLocation().c_str());
			media.destroy();
		}
	} catch (soci::soci_error const & e) {
		wxLogError
			(_("MediaUpdater::ProcessFiles:%s"), e.what());
	}

	return true;
}

